package cn.tx.erp.sys.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/1/30 17:08
 */
public class TreeNodeBuilder {
    /**
     * 把没有层级关系的集合变成有关系的层级集合
     * @param treeNodes
     * @param topPid
     * @return
     */
    public static List<TreeNode> build(List<TreeNode> treeNodes, Integer topPid) {
        List<TreeNode> nodes = new ArrayList<>();

        for (TreeNode n1 : treeNodes) {
            if (n1.getPid() == topPid) {
                nodes.add(n1);
            }
            for (TreeNode n2 : treeNodes) {
                if (n1.getId() == n2.getPid()) {
                    n1.getChildren().add(n2);
                }
            }
        }
        return nodes;
    }
}
